//定位当前城市
//当前城市名称 浙江省杭州市
// console.log(returnCitySN.cname);

$(function() {
    //从缓存中获取数据（在登录成功后会将当前用户和用户令牌放入缓存中）
    const user = JSON.parse(window.sessionStorage.getItem("user"));
    const token = window.sessionStorage.getItem("token");
    if(user == null || token == null){
        alert("请先登录");
        window.location.href = "../html/welcome.html";
    }

    //导航球 下拉
    $("#gooey").gooeymenu({
        bgColor: "rgba(79,117,167,0.4)",
        contentColor: "white",
        style: "vertical",
        horizontal: {
            menuItemPosition: "glue"
        },
        vertical: {
            menuItemPosition: "spaced",
            direction: "down"
        },
        circle: {
            radius: 90
        },
        margin: "small",
        size: 70,
        bounce: true,
        bounceLength: "small",
        transitionStep: 100,
        hover: "rgba(79, 117, 167, 0.6)"
    });

    //滚动条
    $(".tucao-content").scrollBar({
        barWidth: 5, //滚动条的宽度(这里根据需要写数值即可，不设置是10,即默认10px)
        position: "y", //写“x”代表只出水平滚动条，写“y”表示只出垂直滚动条，写“x,y”则出水平和垂直滚动条（只有在内容超出容器时才出现滚动条）
        wheelDis: 60 //滚轮滚动一次向下或向上滚动的距离，默认是15，可根据需要修改数值
    });

    //初始化时默认获取我的吐槽数据
    getMyTucao(token,1,user.username,user.userId);

    //给左侧标签页添加点击事件
    $("#tucao").click(function (){
        getMyTucao(token,1,user.username,user.userId);
        $("#tucaoPoint").removeAttr("class");
        $("#tucaoPoint").attr("class","card bg-01 active");
        $("#heartPoint").removeAttr("class");
        $("#heartPoint").attr("class","card bg-02");
        $("#likePoint").removeAttr("class");
        $("#likePoint").attr("class","card bg-02");
    });
    $("#heart").click(function (){
        getMyHeart(token,1,user.username,user.userId);
        $("#tucaoPoint").removeAttr("class");
        $("#tucaoPoint").attr("class","card bg-01");
        $("#heartPoint").removeAttr("class");
        $("#heartPoint").attr("class","card bg-02 active");
        $("#likePoint").removeAttr("class");
        $("#likePoint").attr("class","card bg-02");
    });
    $("#like").click(function (){
        getMyLike(token,1,user.userId);
        $("#tucaoPoint").removeAttr("class");
        $("#tucaoPoint").attr("class","card bg-01");
        $("#heartPoint").removeAttr("class");
        $("#heartPoint").attr("class","card bg-02");
        $("#likePoint").removeAttr("class");
        $("#likePoint").attr("class","card bg-02 active");
    });

    //关闭页面时调用logout方法，退出当前登录用户
    $(window).unload(function(){
        $.ajax({
            type:"POST",
            contentType:"application/json",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            url :"http://localhost:2082/logout?username=" + user.username
        })
    });


});


/**
 * 开关；true->吐槽;false->鸡汤
 */
let tucaoOrHeartFlag = true;

/**
 * ====================================================================================以下是我的吐槽======================================================================================
 */

/**
 * 分页获取我发布的吐槽数据
 * @param token 令牌
 * @param current 页码
 * @param username 用户名
 * @param userId 用户id
 */
function getMyTucao(token,current,username,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/manage-my/tucao?username=" + username + "&current=" + current,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    //设置开关为吐槽
                    tucaoOrHeartFlag = true;

                    const pageData = result.data;
                    //records：获取到的分页数组数据
                    const records = eval("("+JSON.stringify(pageData)+")").records;
                    //total：所有数据的数量
                    const total = eval("("+JSON.stringify(pageData)+")").total;
                    //size：每页数据的数量
                    const size = eval("("+JSON.stringify(pageData)+")").size;
                    //current：当前页数
                    const current = eval("("+JSON.stringify(pageData)+")").current;
                    //总页数
                    const pageNum = eval("("+JSON.stringify(pageData)+")").pages;

                    //先清空数据
                    for (let i = 0; i < 10; i++) {
                        $("#username" + i).empty();
                        $("#verifyClassify" + i).empty();
                        $("#contentClassify" + i).empty();
                        $("#tucaoSort" + i).empty();
                        $("#time" + i).empty();
                        $("#place" + i).empty();
                        $("#id" + i).empty();
                        $("#content" + i).empty();
                        $("#likeNum" + i).empty();
                        $("#discussNum" + i).empty();
                    }
                    //再进行赋值
                    let tucaoNum;
                    if( (total-size*(current-1)) >= 10){
                        tucaoNum = 10;
                    }else if( (total-size*(current-1)) < 10){
                        tucaoNum = total-size*(current-1);
                        for (let i = tucaoNum; i < 10; i++) {
                            //添加不可见样式
                            $("#tucao" + i).attr("style","display:none");
                        }
                    }
                    for (let i = 0; i < tucaoNum; i++) {
                        //移除不可见样式
                        $("#tucao" + i).removeAttr("style");
                        //审核是否通过
                        $("#verifyClassify" + i).removeAttr("style");
                        $("#verifyClassify" + i).removeAttr("class");
                        //吐槽或鸡汤
                        $("#contentClassify" + i).removeAttr("style");
                        $("#contentClassify" + i).removeAttr("class");
                        $("#contentClassify" + i).attr("class","contentSortTucao");
                        $("#contentClassify" + i).html("吐槽");
                        //显示删除按钮
                        $(".deleteMine").removeAttr("style");
                        //吐槽类别
                        $("#tucaoSort" + i).removeAttr("style");

                        const tucaoId = JSON.stringify(records[i].tucaoId);
                        const usernameFromData = JSON.stringify(records[i].username);
                        const tucaoContent = JSON.stringify(records[i].content);
                        const likeNum = JSON.stringify(records[i].likeNum);
                        const discussNum = JSON.stringify(records[i].discussNum);
                        const place = JSON.stringify(records[i].place);
                        const sort = JSON.stringify(records[i].sort);
                        const time = JSON.stringify(records[i].createTime);
                        const verify = JSON.stringify(records[i].verify);

                        if(verify == 0){
                            $("#verifyClassify" + i).attr("class","noVerify");
                            $("#verifyClassify" + i).html("未审核");
                        }else if(verify == 1){
                            $("#verifyClassify" + i).attr("class","verifySucceed");
                            $("#verifyClassify" + i).html("审核通过");
                        }else if(verify == 2){
                            $("#verifyClassify" + i).attr("class","verifyFailed");
                            $("#verifyClassify" + i).html("审核未通过");
                        }
                        $("#tucaoSort" + i).html(sort.replace(/^\"|\"$/g,''));
                        $("#username"+i).html(usernameFromData.replace(/^\"|\"$/g,''));
                        $("#time"+i).html(renderTime(time.replace(/^\"|\"$/g,'')));
                        $("#place"+i).html(place.replace(/^\"|\"$/g,''));
                        $("#id"+i).html(tucaoId.replace(/^\"|\"$/g,''));
                        $("#content"+i).html(tucaoContent.replace(/^\"|\"$/g,''));
                        $("#likeNum"+i).html(likeNum.replace(/^\"|\"$/g,''));
                        $("#discussNum"+i).html(discussNum.replace(/^\"|\"$/g,''));

                        //获取当前用户是否对该吐槽进行过点赞操作
                        judgeIsLike(token,userId,tucaoId,0,i);

                        //给删除按钮解绑点击事件
                        $("#deleteMine" + i).off("click");
                        //给删除按钮添加点击事件
                        $("#deleteMine" + i).click(function (){
                            delTucaoOrHeart(token,tucaoId,true,username,userId);
                        });
                    }

                    //设置底部分页栏
                    $("#pagination").pagination({
                        currentPage: current,
                        totalPage: pageNum,
                        isShow: true,
                        count: 10,
                        homePageText: "首页",
                        endPageText: "尾页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        callback: function(current) {
                            tucaoCurrentPage = current;
                            getMyTucao(token,current,username,userId);
                        }
                    });

                    //设置点赞、取消点赞、跳转评论界面等功能；
                    setLikeAndSkip(token,userId);
                }
                //操作失败
                else if(result.code == 500) {
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    })
}

/**
 * 吐槽 当前页码
 */
let tucaoCurrentPage = 1;

/**
 * 点赞吐槽实体类
 * @param userId 用户id
 * @param tucaoId 用户点赞的吐槽id
 * @param sort 分类；0->吐槽；1->鸡汤
 */
function TucaoLike(userId,tucaoId,sort){
    this.userId = userId;
    this.tucaoId = tucaoId;
    this.sort = sort;
}

/**
 * 点赞吐槽 ajax
 * @param token 令牌
 * @param tucaoLike 吐槽点赞实体类
 * @param userId 用户id
 * @param tucaoId 吐槽id
 * @param i 排序值
 */
function likeTucao(token,tucaoLike,userId,tucaoId,i){
    $.ajax({
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(tucaoLike),
        headers: {
            "token":token
        },
        url: "http://localhost:2082/user-like/like",
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    judgeIsLike(token,userId,tucaoId,0,i);
                }
                //操作失败
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    });
}

/**
 * 取消对某条吐槽的点赞
 * @param token 令牌
 * @param userId 用户id
 * @param tucaoId 吐槽或鸡汤id
 * @param i 排序
 */
function cancelLikeTucao(token,userId,tucaoId,i){
    $.ajax({
        type: "POST",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/user-like/cancel-like?userId=" + userId +"&tucaoOrHeartId=" + tucaoId + "&sort=0",
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    judgeIsLike(token,userId,tucaoId,0,i);
                }
                //操作失败
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    });
}


/**
 * ====================================================================================以下是我的鸡汤======================================================================================
 */

/**
 * 分页获取我发布的鸡汤数据
 * @param token 令牌
 * @param current 页码
 * @param username 用户名
 * @param userId 用户id
 */
function getMyHeart(token,current,username,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/manage-my/heart?username=" + username + "&current=" + current,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    //设置开关为鸡汤
                    tucaoOrHeartFlag = false;

                    const pageData = result.data;
                    //records：获取到的分页数组数据
                    const records = eval("("+JSON.stringify(pageData)+")").records;
                    //total：所有数据的数量
                    const total = eval("("+JSON.stringify(pageData)+")").total;
                    //size：每页数据的数量
                    const size = eval("("+JSON.stringify(pageData)+")").size;
                    //current：当前页数
                    const current = eval("("+JSON.stringify(pageData)+")").current;
                    //总页数
                    const pageNum = eval("("+JSON.stringify(pageData)+")").pages;

                    //先清空数据
                    for (let i = 0; i < 10; i++) {
                        $("#username" + i).empty();
                        $("#verifyClassify" + i).empty();
                        $("#contentClassify" + i).empty();
                        $("#tucaoSort" + i).empty();
                        $("#time" + i).empty();
                        $("#place" + i).empty();
                        $("#id" + i).empty();
                        $("#content" + i).empty();
                        $("#likeNum" + i).empty();
                        $("#discussNum" + i).empty();
                    }
                    //再进行赋值
                    let heartNum;
                    if( (total-size*(current-1)) >= 10){
                        heartNum = 10;
                    }else if( (total-size*(current-1)) < 10){
                        heartNum = total-size*(current-1);
                        for (let i = heartNum; i < 10; i++) {
                            //添加不可见样式
                            $("#tucao" + i).attr("style","display:none");
                        }
                    }
                    for (let i = 0; i < heartNum; i++) {
                        //移除不可见样式
                        $("#tucao" + i).removeAttr("style");
                        //审核是否通过
                        $("#verifyClassify" + i).removeAttr("style");
                        $("#verifyClassify" + i).removeAttr("class");
                        //吐槽或鸡汤
                        $("#contentClassify" + i).removeAttr("style");
                        $("#contentClassify" + i).removeAttr("class");
                        $("#contentClassify" + i).attr("class","contentSortHeart");
                        $("#contentClassify" + i).html("鸡汤");
                        //显示删除按钮
                        $(".deleteMine").removeAttr("style");
                        //隐藏吐槽类别
                        $("#tucaoSort" + i).attr("style","display: none");

                        const heartId = JSON.stringify(records[i].id);
                        const usernameFromData = JSON.stringify(records[i].username);
                        const heartContent = JSON.stringify(records[i].content);
                        const likeNum = JSON.stringify(records[i].likeNum);
                        const discussNum = JSON.stringify(records[i].discussNum);
                        const place = JSON.stringify(records[i].place);
                        const time = JSON.stringify(records[i].createTime);
                        const verify = JSON.stringify(records[i].verify);

                        if(verify == 0){
                            $("#verifyClassify" + i).attr("class","noVerify");
                            $("#verifyClassify" + i).html("未审核");
                        }else if(verify == 1){
                            $("#verifyClassify" + i).attr("class","verifySucceed");
                            $("#verifyClassify" + i).html("审核通过");
                        }else if(verify == 2){
                            $("#verifyClassify" + i).attr("class","verifyFailed");
                            $("#verifyClassify" + i).html("审核未通过");
                        }
                        $("#username"+i).html(usernameFromData.replace(/^\"|\"$/g,''));
                        $("#time"+i).html(renderTime(time.replace(/^\"|\"$/g,'')));
                        $("#place"+i).html(place.replace(/^\"|\"$/g,''));
                        $("#id"+i).html(heartId.replace(/^\"|\"$/g,''));
                        $("#content"+i).html(heartContent.replace(/^\"|\"$/g,''));
                        $("#likeNum"+i).html(likeNum.replace(/^\"|\"$/g,''));
                        $("#discussNum"+i).html(discussNum.replace(/^\"|\"$/g,''));

                        //获取当前用户是否对该吐槽进行过点赞操作
                        judgeIsLike(token,userId,heartId,1,i);

                        //给删除按钮解绑点击事件
                        $("#deleteMine" + i).off("click");
                        //给删除按钮添加点击事件
                        $("#deleteMine" + i).click(function (){
                            delTucaoOrHeart(token,heartId,false,username,userId);
                        });
                    }

                    //设置底部分页栏
                    $("#pagination").pagination({
                        currentPage: current,
                        totalPage: pageNum,
                        isShow: true,
                        count: 10,
                        homePageText: "首页",
                        endPageText: "尾页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        callback: function(current) {
                            heartCurrentPage = current;
                            getMyHeart(token,current,username,userId);
                        }
                    });

                    //设置点赞、取消点赞、跳转评论界面等功能；
                    setLikeAndSkip(token,userId);

                }
                //操作失败
                else if(result.code == 500) {
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    })
}

/**
 * 鸡汤 当前页码
 */
let heartCurrentPage = 1;

/**
 * 点赞鸡汤实体类
 * @param userId 用户id
 * @param heartId 用户点赞的鸡汤id
 * @param sort 分类；0->吐槽；1->鸡汤
 */
function HeartLike(userId,heartId,sort){
    this.userId = userId;
    this.heartId = heartId;
    this.sort = sort;
}

/**
 * 点赞鸡汤 ajax
 * @param token 令牌
 * @param heartLike 鸡汤点赞实体类
 * @param userId 用户id
 * @param heartId 吐槽id
 * @param i 排序值
 */
function likeHeart(token,heartLike,userId,heartId,i){
    $.ajax({
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(heartLike),
        headers: {
            "token":token
        },
        url: "http://localhost:2082/user-like/like",
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    judgeIsLike(token,userId,heartId,1,i);
                }
                //操作失败
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    });
}

/**
 * 取消对某条鸡汤的点赞
 * @param token 令牌
 * @param userId 用户id
 * @param heartId 吐槽或鸡汤id
 * @param i 排序
 */
function cancelLikeHeart(token,userId,heartId,i) {
    $.ajax({
        type: "POST",
        contentType: "application/json",
        headers: {
            "token": token
        },
        url: "http://localhost:2082/user-like/cancel-like?userId=" + userId + "&tucaoOrHeartId=" + heartId + "&sort=1",
        success: function (data, status, xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if (responseToken == "overdue") {
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            } else {
                let result = JSON.stringify(data);
                result = eval("(" + result + ")");
                //操作成功
                if (result.code == 200) {
                    judgeIsLike(token, userId, heartId, 1, i);
                }
                //操作失败
                else if (result.code == 500) {
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    });
}

/**
 * ====================================================================================以下是我的赞======================================================================================
 */

/**
 * 分页获取我点赞过的数据
 * @param token 令牌
 * @param current 页码
 * @param userId 用户id
 */
function getMyLike(token,current,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/manage-my/like?userId=" + userId + "&current=" + current,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    const pageData = result.data;
                    //records：获取到的分页数组数据
                    const records = eval("("+JSON.stringify(pageData)+")").records;
                    //total：所有数据的数量
                    const total = eval("("+JSON.stringify(pageData)+")").total;
                    //size：每页数据的数量
                    const size = eval("("+JSON.stringify(pageData)+")").size;
                    //current：当前页数
                    const current = eval("("+JSON.stringify(pageData)+")").current;
                    //总页数
                    const pageNum = eval("("+JSON.stringify(pageData)+")").pages;

                    //先清空数据
                    for (let i = 0; i < 10; i++) {
                        $("#username" + i).empty();
                        $("#verifyClassify" + i).empty();
                        $("#contentClassify" + i).empty();
                        $("#tucaoSort" + i).empty();
                        $("#time" + i).empty();
                        $("#place" + i).empty();
                        $("#id" + i).empty();
                        $("#content" + i).empty();
                        $("#likeNum" + i).empty();
                    }

                    //再进行赋值
                    let likeNum;
                    if( (total-size*(current-1)) >= 10){
                        likeNum = 10;
                    }else if( (total-size*(current-1)) < 10){
                        likeNum = total-size*(current-1);
                        for (let i = likeNum; i < 10; i++) {
                            //添加不可见样式
                            $("#tucao" + i).attr("style","display:none");
                        }
                    }
                    for (let i = 0; i < likeNum; i++) {
                        // //移除不可见样式
                        // $("#tucao" + i).removeAttr("style");
                        //移除审核样式
                        $("#verifyClassify" + i).attr("style","display:none");
                        //移除吐槽或鸡汤样式
                        $("#contentClassify" + i).removeAttr("style");
                        $("#contentClassify" + i).removeAttr("class");
                        //移除吐槽类别样式
                        $("#tucaoSort" + i).attr("style","display: none");
                        //移除删除按钮
                        $(".deleteMine").attr("style","display: none");

                        //判断是鸡汤还是吐槽
                        const heartId = JSON.stringify(records[i].heartId);
                        const tucaoId = JSON.stringify(records[i].tucaoId);
                        if(heartId != null && heartId != "null"){
                            $("#contentClassify" + i).attr("class","contentSortHeart");
                            $("#contentClassify"+i).empty();
                            $("#contentClassify" + i).html("鸡汤");
                            //根据heartId获取鸡汤数据
                            getHeartContent(token,i,heartId,userId);
                            //给这条鸡汤数据的点赞按钮添加点赞、取消点赞、评论跳转的功能
                            //先移除绑定
                            $("#likeBtn" + i).off("click");
                            //再添加点击事件
                            $("#likeBtn"+i).click(function (){
                                const likeCronClass = $("#likeCron" + i).attr("class");
                                const heartId = $("#id" + i).text();
                                const likeNum = $("#likeNum"+i).text();
                                //点赞按钮是空心的 未点赞
                                if(likeCronClass == "fa fa-heart-o"){
                                    const heartLike = new HeartLike(userId,heartId,1);
                                    likeHeart(token,heartLike,userId,heartId,i);
                                    //点赞数加一
                                    $("#likeNum"+i).empty();
                                    $("#likeNum"+i).html(likeNum*1 + 1);
                                }
                                //点赞按钮是实心的 已点赞
                                else if(likeCronClass == "fa fa-heart"){
                                    // 点赞数减一
                                    $("#likeNum"+i).empty();
                                    $("#likeNum"+i).html(likeNum*1 - 1);
                                    cancelLikeHeart(token,userId,heartId,i);
                                }
                            });
                            //跳转到评论界面
                            $("#content" + i).click(function (){
                                location.href = "discuss.html?username=" + $("#username" + i).text() + "&time=" + $("#time" + i).text() +
                                    "&place=" + $("#place" + i).text() + "&heartContent=" + $("#content" + i).text() +
                                    "&heartId=" + $("#id" + i).text() +"&tucaoOrHeart=heart";
                            });
                        }
                        if(tucaoId != null && tucaoId != "null"){
                            $("#contentClassify" + i).attr("class","contentSortTucao");
                            $("#contentClassify"+i).empty();
                            $("#contentClassify" + i).html("吐槽");
                            //使吐槽分类可见
                            $("#tucaoSort" + i).removeAttr("style");
                            //根据tucaoId获取吐槽数据
                            getTucaoContent(token,i,tucaoId,userId);
                            //给这条吐槽数据的点赞按钮添加点赞、取消点赞、评论跳转的功能
                            //先移除绑定
                            $("#likeBtn" + i).off("click");
                            //再添加点击事件
                            $("#likeBtn"+i).click(function (){
                                const likeCronClass = $("#likeCron" + i).attr("class");
                                const tucaoId = $("#id" + i).text();
                                const likeNum = $("#likeNum"+i).text();
                                //点赞按钮是空心的 未点赞
                                if(likeCronClass == "fa fa-heart-o"){
                                    const tucaoLike = new TucaoLike(userId,tucaoId,0);
                                    likeTucao(token,tucaoLike,userId,tucaoId,i);
                                    //点赞数加一
                                    $("#likeNum"+i).empty();
                                    $("#likeNum"+i).html(likeNum*1 + 1);
                                }
                                //点赞按钮是实心的 已点赞
                                else if(likeCronClass == "fa fa-heart"){
                                    cancelLikeTucao(token,userId,tucaoId,i);
                                    // 点赞数减一
                                    $("#likeNum"+i).empty();
                                    $("#likeNum"+i).html(likeNum*1 - 1);
                                }
                            });
                            //跳转到评论界面
                            $("#content" + i).click(function (){
                                location.href = "discuss.html?username=" + $("#username" + i).text() + "&time=" + $("#time" + i).text() +
                                    "&place=" + $("#place" + i).text() + "&heartContent=" + $("#content" + i).text() +
                                    "&heartId=" + $("#id" + i).text() +"&tucaoOrHeart=tucao";
                            });
                        }
                    }

                    //设置底部分页栏
                    $("#pagination").pagination({
                        currentPage: current,
                        totalPage: pageNum,
                        isShow: true,
                        count: 10,
                        homePageText: "首页",
                        endPageText: "尾页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        callback: function(current) {
                            likeCurrentPage = current;
                            getMyLike(token,likeCurrentPage,userId);
                        }
                    });

                }
                //操作失败
                else if(result.code == 500) {
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    })
}

/**
 * 我的赞 当前页码
 */
let likeCurrentPage = 1;

/**
 * 根据tucaoId获取吐槽数据
 * @param token 令牌
 * @param i 排序
 * @param tucaoId 吐槽id
 * @param userId 用户id
 */
function getTucaoContent(token,i,tucaoId,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/tucao/one?tucaoId=" + tucaoId,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    //移除不可见样式
                    $("#tucao" + i).removeAttr("style");

                    const tucaoData = result.data;
                    const username = JSON.stringify(tucaoData.username);
                    const tucaoId = JSON.stringify(tucaoData.tucaoId);
                    const tucaoContent = JSON.stringify(tucaoData.content);
                    const likeNum = JSON.stringify(tucaoData.likeNum);
                    const discussNum = JSON.stringify(tucaoData.discussNum);
                    const createTime = JSON.stringify(tucaoData.createTime);
                    const place = JSON.stringify(tucaoData.place);
                    const sort = JSON.stringify(tucaoData.sort);

                    //先清空数据
                    $("#username"+i).empty();
                    $("#verifyClassify"+i).empty();
                    $("#tucaoSort"+i).empty();
                    $("#time"+i).empty();
                    $("#place"+i).empty();
                    $("#id"+i).empty();
                    $("#content"+i).empty();
                    $("#likeNum"+i).empty();
                    $("#discussNum"+i).empty();

                    $("#username"+i).html(username.replace(/^\"|\"$/g,''));
                    $("#time"+i).html(renderTime(createTime.replace(/^\"|\"$/g,'')));
                    $("#place"+i).html(place.replace(/^\"|\"$/g,''));
                    $("#id"+i).html(tucaoId.replace(/^\"|\"$/g,''));
                    $("#content"+i).html(tucaoContent.replace(/^\"|\"$/g,''));
                    $("#likeNum"+i).html(likeNum.replace(/^\"|\"$/g,''));
                    $("#discussNum"+i).html(discussNum.replace(/^\"|\"$/g,''));
                    $("#tucaoSort"+i).html(sort.replace(/^\"|\"$/g,''));

                    //获取当前用户是否对该吐槽进行过点赞操作
                    judgeIsLike(token,userId,tucaoId,0,i);
                }
                //操作失败
                else if(result.code == 500) {
                    //设置不可见样式
                    $("#tucao" + i).attr("style","display:none");
                }
            }
        }
    })
}

/**
 * 根据heartId获取鸡汤数据
 * @param token 令牌
 * @param i 排序
 * @param heartId 鸡汤id
 * @param userId 用户id
 */
function getHeartContent(token,i,heartId,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/heart/one?heartId=" + heartId,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    //移除不可见样式
                    $("#tucao" + i).removeAttr("style");

                    const heartData = result.data;
                    const username = JSON.stringify(heartData.username);
                    const heartId = JSON.stringify(heartData.id);
                    const heartContent = JSON.stringify(heartData.content);
                    const likeNum = JSON.stringify(heartData.likeNum);
                    const discussNum = JSON.stringify(heartData.discussNum);
                    const createTime = JSON.stringify(heartData.createTime);
                    const place = JSON.stringify(heartData.place);

                    //先清空数据
                    $("#username"+i).empty();
                    $("#verifyClassify"+i).empty();
                    $("#tucaoSort"+i).empty();
                    $("#time"+i).empty();
                    $("#place"+i).empty();
                    $("#id"+i).empty();
                    $("#content"+i).empty();
                    $("#likeNum"+i).empty();
                    $("#discussNum"+i).empty();

                    $("#username"+i).html(username.replace(/^\"|\"$/g,''));
                    $("#time"+i).html(renderTime(createTime.replace(/^\"|\"$/g,'')));
                    $("#place"+i).html(place.replace(/^\"|\"$/g,''));
                    $("#id"+i).html(heartId.replace(/^\"|\"$/g,''));
                    $("#content"+i).html(heartContent.replace(/^\"|\"$/g,''));
                    $("#likeNum"+i).html(likeNum.replace(/^\"|\"$/g,''));
                    $("#discussNum"+i).html(discussNum.replace(/^\"|\"$/g,''));

                    //获取当前用户是否对该吐槽进行过点赞操作
                    judgeIsLike(token,userId,heartId,1,i);
                }
                //操作失败
                else if(result.code == 500) {
                    //设置不可见样式
                    $("#tucao" + i).attr("style","display:none");
                }
            }
        }
    })
}

/**
 * ====================================================================================以下是公用方法======================================================================================
 */
function setLikeAndSkip(token,userId){
    if(tucaoOrHeartFlag){
        for (let i = 0; i < 10; i++) {
            //先移除绑定
            $("#likeBtn" + i).off("click");
            //添加点赞和取消点赞功能
            $("#likeBtn" + i).click(function (){
                const likeCronClass = $("#likeCron" + i).attr("class");
                const tucaoId = $("#id" + i).text();
                const likeNum = $("#likeNum"+i).text();
                //点赞按钮是空心的 未点赞
                if(likeCronClass == "fa fa-heart-o"){
                    const tucaoLike = new TucaoLike(userId,tucaoId,0);
                    likeTucao(token,tucaoLike,userId,tucaoId,$("#sortId"+i).text());
                    //点赞数加一
                    $("#likeNum"+i).empty();
                    $("#likeNum"+i).html(likeNum*1 + 1);
                }
                //点赞按钮是实心的 已点赞
                else if(likeCronClass == "fa fa-heart"){
                    cancelLikeTucao(token,userId,tucaoId,$("#sortId"+i).text());
                    // 点赞数减一
                    $("#likeNum"+i).empty();
                    $("#likeNum"+i).html(likeNum*1 - 1);
                }
            });

            //添加点击跳转评论界面功能
            $("#content" + i).click(function (){
                location.href = "discuss.html?username=" + $("#username" + i).text() + "&time=" + $("#time" + i).text() +
                    "&place=" + $("#place" + i).text() + "&tucaoContent=" + $("#content" + i).text() +
                    "&tucaoId=" + $("#id" + i).text() +"&tucaoOrHeart=tucao";
            });
        }
    }else {
        for (let i = 0; i < 10; i++) {
            //先移除绑定
            $("#likeBtn" + i).off("click");
            //添加点赞和取消点赞功能
            $("#likeBtn" + i).click(function (){
                const likeCronClass = $("#likeCron" + i).attr("class");
                const heartId = $("#id" + i).text();
                const likeNum = $("#likeNum"+i).text();
                //点赞按钮是空心的 未点赞
                if(likeCronClass == "fa fa-heart-o"){
                    const heartLike = new HeartLike(userId,heartId,1);
                    likeHeart(token,heartLike,userId,heartId,$("#sortId"+i).text());
                    //点赞数加一
                    $("#likeNum"+i).empty();
                    $("#likeNum"+i).html(likeNum*1 + 1);
                }
                //点赞按钮是实心的 已点赞
                else if(likeCronClass == "fa fa-heart"){
                    cancelLikeHeart(token,userId,heartId,$("#sortId"+i).text());
                    //点赞数减一
                    $("#likeNum"+i).empty();
                    $("#likeNum"+i).html(likeNum*1 - 1);
                }
            });
            //添加点击跳转评论界面功能
            $("#content" + i).click(function (){
                location.href = "discuss.html?username=" + $("#username" + i).text() + "&time=" + $("#time" + i).text() +
                    "&place=" + $("#place" + i).text() + "&heartContent=" + $("#content" + i).text() +
                    "&heartId=" + $("#id" + i).text() +"&tucaoOrHeart=heart";
            });
        }
    }
}

/**
 * 通过userId和tucaoId，判断当前用户是否对这条吐槽进行过点赞操作
 * @param token 令牌
 * @param userId 用户id
 * @param tucaoOrHeartId 吐槽id或鸡汤id
 * @param sort 分类；0->吐槽；1->鸡汤
 * @param i 当前进行判断的吐槽数据的排序（例如：点赞页面中展示的第一条吐槽数据，i=0）
 */
function judgeIsLike(token,userId,tucaoOrHeartId,sort,i){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/user-like/judge?userId=" + userId + "&tucaoOrHeartId=" + tucaoOrHeartId + "&sort=" + sort,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //表示该吐槽已被当前用户点赞
                if(result.code == 200){
                    //先移除class
                    $("#likeCron"+i).removeAttr("class");
                    //再添加class 实心
                    $("#likeCron"+i).attr("class","fa fa-heart");
                }
                //未点赞
                else if(result.code == 500){
                    //先移除class
                    $("#likeCron"+i).removeAttr("class");
                    //再添加class 空心
                    $("#likeCron"+i).attr("class","fa fa-heart-o");
                }
            }
        }
    });
}

/**
 * 删除吐槽或鸡汤
 * @param token
 * @param id
 * @param flag
 * @param username
 * @param userId
 */
function delTucaoOrHeart(token,id,flag,username,userId){
    $.ajax({
        type: "PUT",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/manage-my/delete?id=" + id + "&flag=" + flag,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                if(result.code == 200){
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                    //重新分页
                    if(flag){
                        getMyTucao(token,tucaoCurrentPage,username,userId);
                    }else {
                        getMyHeart(token,heartCurrentPage,username,userId);
                    }
                }
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    });
}

/**
 * 时间格式转换
 * @param date
 * @returns {string}
 */
function renderTime(date) {
    var dateee = new Date(date).toJSON();
    // return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
    return new Date(+new Date(dateee)).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
}


